<?php
/**
 * MessageGateway IM
 * @author seraph.liujb 2009.07.15
 *
 */
/**
 * 应用示例
 //取得好友列表
 var_dump('Roster:');
 $mg = Common_MessageGateway::factory('IM');
 $username = 'test';
 $password = 'test';
 $mg->setAccout($username,$password);
 $result = $mg->getRoster();
 var_dump($result);

 //发送消息
 var_dump('Send message:');
 $mg = Common_MessageGateway::factory('IM');
 $username = 'test';
 $password = 'test';
 $mg->setAccout($username,$password)->setTo('seraph@testweb.idkin.com/web');
 $result = $mg->sendMessage('测试消息，来自：' . $username . '，' . rand(0,1000));
 var_dump($result);

 sleep(2);
 //接收消息
 var_dump('Receive message:');
 $mg = Common_MessageGateway::factory('IM');
 $username = 'test';
 $password = 'test';
 $mg->setAccout($username,$password);
 $result = $mg->getMessage();
 var_dump($result);
 */

class Common_MessageGateway_IM
{
	/**
	 * 即时通讯服务地址，一般无需重设
	 * @var string $_url
	 */
	protected $_url = "http://192.168.10.254:8080/Service/IM";

	/**
	 * 帐号名称
	 * @var string $_username
	 */
	private $_username;

	/**
	 * 密码
	 * @var string $_password
	 */
	private $_password;

	/**
	 * 接收消息的用户
	 * @var string $_to
	 */
	private $_to;

	/**
	 * 初始化处理
	 *
	 * @param array $params 初始化参数
	 * url 重设即时通讯服务地址，一般用于服务分布或连接不同应用模块的服务
	 */
	public function __construct($params = array())
	{
		//设置参数
		if (!is_null($params['url'])) $this->_url = $params['url'];
		//
		$this->_logger = Common_Logger::factory(array('module'=>'Common_MessageGateway_IM'));
	}

	/**
	 * 设置消息发送的目的帐户
	 *
	 * @param    string $user
	 * @return   object $this
	 */
	public function setTo($user)
	{
		$this->_to = $user;
		return $this;
	}

	/**
	 * 设置消息发送的来源帐户，一般无需设置
	 *
	 * @param    string $user
	 * @return   object $this
	 */
	public function setFrom($user)
	{
		/* @todo 此处基类方法还未实现 */
//		parent::setFrom($user);
		return $this;
	}

	/**
	 * 设置当前用户的的帐号密码，以建立服务器连接
	 *
	 * @param    string $username
	 * @param    string $password
	 * @return   object $this
	 */
	public function setAccout($username, $password)
	{
		$this->_username = $username;
		$this->_password = $password;
		return $this;
	}

	/**
	 * 发送信息
	 *
	 * @param    string $message
	 * @return   boolean
	 */
	public function sendMessage($message)
	{
		$this->_message = $message;
		$result = $this->_parseXML($this->_send($this->_xml("send")));
		$result = $result['body'];
		return $result;
	}

	/**
	 * 取回消息
	 *
	 * @return   array
	 */
	public function getMessage()
	{
		$result = $this->_parseXML($this->_send($this->_xml("receive")));
		$result = $result['body'];
		if($result['count'] == 1){
			$temp = $result['message'];
			$result['message'] = array();
			$result['message'][0] = $temp;
		}
		return $result;
	}

	/**
	 * 取得好友列表
	 *
	 * @return   array
	 */
	public function getRoster()
	{
		$result = $this->_parseXML($this->_send($this->_xml("roster")));
		$result = $result['body'];
		//当count为1，整理roster为统一数组结构
		if (1 ==$result['count'] && !is_array($result['roster'][0])) {
			$temp = $result['roster'];
			$result['roster'] = array();
			$result['roster'][0] = $temp;
		}
		return $result;
	}

	/**
	 * 构造XML代码
	 * @param string $type 方法代码
	 * @return string $xml XML请求代码
	 */
	private function _xml($type){
		$xml = "<body function=\"".$type."\">";
		$xml .= "<username>".$this->_username."</username>";
		$xml .= "<password>".$this->_password."</password>";
		$xml .= "<to>".$this->_to."</to>";
		switch ($type){
			case "send":
				$xml .= "<message>".$this->_message."</message>";
				break;
			case "receive":
				break;
			case "roster":
				$xml .= "<status>all</status>";
				break;
			default:
				return null;
		}
		$xml .= "</body>";
		return $xml;
	}

	/**
	 * 发送XML代码 
	 * @param string $xml XML请求代码
	 * @return string XML 请求返回信息
	 */
	private function _send($xml){
		$client = new Zend_Http_Client($this->_url);
		$client->setMethod(Zend_Http_Client::POST);
		$client->setRawData($xml);
		$response = $client->request();
		return $response->getBody();
	}

	/**
	 * 解析XML 
	 * @param string $xml XML请求代码
	 * @return array;
	 */
	private function _parseXML($xml){
		return Zend_Json::decode(Zend_Json::fromXml($xml));
	}
	
}
